package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.mapper.CourseBaseMapper;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.model.dto.AddTeacherDto;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.model.po.CourseTeacher;
import com.xuecheng.content.service.CourseTeacherService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CourseTeacherServiceImpl implements CourseTeacherService {

    @Autowired
    private CourseTeacherMapper courseTeacherMapper;

    @Autowired
    private CourseBaseMapper courseBaseMapper;

    /**
     * 根据课程id查询教师信息
     * @param id
     * @return
     */
    public List<CourseTeacher> getTeacherListByCourseId(Long id) {
        //查询条件
        LambdaQueryWrapper<CourseTeacher> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CourseTeacher::getCourseId,id);
        List<CourseTeacher> courseTeachers = courseTeacherMapper.selectList(wrapper);
        return courseTeachers;
    }

    @Transactional
    public CourseTeacher saveOrUpdateTeacher(AddTeacherDto teacherDto, Long companyId) {
        //参数合法性校验
        if(teacherDto == null){
            XueChengPlusException.cast("添加教师信息为空");
        }
        if(companyId == null){
            XueChengPlusException.cast("机构id为空");
        }
        //查询该课程是否为当前机构课程
        Long courseId = teacherDto.getCourseId();
        CourseBase courseBase = courseBaseMapper.selectById(courseId);
        //该课程机构id
        Long company = courseBase.getCompanyId();

        if(!company.equals(companyId)){
            XueChengPlusException.cast("该课程不属于该机构");
        }
        //保存或更新
        if(teacherDto.getId()==null){
            //添加
            CourseTeacher courseTeacher = new CourseTeacher();
            BeanUtils.copyProperties(teacherDto,courseTeacher);
            courseTeacher.setCreateDate(LocalDateTime.now());
            int i = courseTeacherMapper.insert(courseTeacher);
            if(i<=0){
                XueChengPlusException.cast("添加教师信息失败");
            }
            return courseTeacher;
        }else {
            //更新
            CourseTeacher courseTeacher = new CourseTeacher();
            BeanUtils.copyProperties(teacherDto,courseTeacher);
            courseTeacher.setCreateDate(LocalDateTime.now());
            int i = courseTeacherMapper.updateById(courseTeacher);
            if(i<=0){
                XueChengPlusException.cast("更新教师信息失败");
            }
            return courseTeacher;
        }

    }

    public void deleteTeacher(Long courseId, Long teacherId, Long companyId) {
        if (courseId == null) {
            XueChengPlusException.cast("课程id为空");
        }
        if (teacherId == null) {
            XueChengPlusException.cast("教师id为空");
        }

        CourseBase courseBase = courseBaseMapper.selectById(courseId);
        //该课程机构id
        Long company = courseBase.getCompanyId();

        if (!company.equals(companyId)) {
            XueChengPlusException.cast("该课程不属于该机构");
        }

        int i = courseTeacherMapper.deleteById(teacherId);
        if (i <= 0) {
            XueChengPlusException.cast("删除教师信息失败");

        }
    }


}
